<?php

namespace backend\controllers;
use Yii;
use yii\web\Controller;

class BaseController extends Controller{
	public function init()
	{
		$session = Yii::$app->session;
		$admin = $session->get('user');
		$sql = 'select CONCAT(controller,"/",action) as node from e_node where node_id in (SELECT node_id from e_role_node where role_id in(SELECT role_id from e_admin_role where admin_id = '.$admin['admin_id'].'))';
		$nodes = Yii::$app->db->createCommand($sql)->queryAll();
		$arr = array();
		foreach($nodes as $key => $val){
			array_push($arr, $val['node']);
		}
		$node = Yii::$app->request->get('r');
		if(empty($admin)){
			$this->goBack();
		}
		if($admin['admin_name'] == 'admin'){
			return true;
		}
		if($node == 'index/index'){
			return true;
		}
		if(!in_array($node, $arr)){
			echo "<script>alert('无权限，brother！');location.href='?r=index/index'</script>";
		}
	}
}